import sys
import qtawesome as qta
from PyQt5 import QtCore, QtGui, QtWidgets, QtSql

class CheckSoft(QtWidgets.QMainWindow):
    windowMinimize = QtCore.pyqtSignal()
    windowMaximize = QtCore.pyqtSignal()
    windowNormal = QtCore.pyqtSignal()
    windowClose = QtCore.pyqtSignal()
    def __init__(self):
        super().__init__()
        self.initUI()
        self.uiStyle()
        self.settingWidgetEvent()

    def initUI(self):
        self.setWindowTitle('SHIT')
        self.setMinimumSize(680,500)
        self.mainWindow = QtWidgets.QWidget()
        self.mainLayout = QtWidgets.QGridLayout()
        self.mainWindow.setLayout(self.mainLayout)

        self.topWindow = QtWidgets.QWidget()
        self.topWindow.setObjectName('top_window')
        self.topLayout = QtWidgets.QHBoxLayout()
        self.topWindow.setLayout(self.topLayout)

        self.midWindow = QtWidgets.QWidget()
        self.midLayout = QtWidgets.QGridLayout()
        self.midWindow.setLayout(self.midLayout)

        self.bottomWindow = QtWidgets.QWidget()
        self.bottomLayout = QtWidgets.QGridLayout()
        self.bottomWindow.setLayout(self.bottomLayout)

        self.mainLayout.addWidget(self.topWindow,0,0,1,6)
        self.mainLayout.addWidget(self.midWindow,1,0,1,6)
        self.mainLayout.addWidget(self.bottomWindow,2,0,1,6)
        self.setCentralWidget(self.mainWindow)

        self.iconLb = QtWidgets.QLabel()
        self.iconLb.setObjectName('icon_label')
        self.iconLb.setText('废柴软件')


        self.newCheckTaskBtn = QtWidgets.QToolButton()
        self.newCheckTaskBtn.setObjectName('top_btn')
        self.newCheckTaskBtn.setText('新建任务')
        self.newCheckTaskBtn.setIcon(QtGui.QIcon('./r1.jpg'))
        self.newCheckTaskBtn.setIconSize(QtCore.QSize(80,80))
        self.newCheckTaskBtn.setToolButtonStyle(QtCore.Qt.ToolButtonTextUnderIcon)

        self.startCheckBtn = QtWidgets.QToolButton()
        self.startCheckBtn.setObjectName('top_btn')
        self.startCheckBtn.setText('质量检查')
        self.startCheckBtn.setIcon(QtGui.QIcon('./r1.jpg'))
        self.startCheckBtn.setIconSize(QtCore.QSize(80,80))
        self.startCheckBtn.setToolButtonStyle(QtCore.Qt.ToolButtonTextUnderIcon)

        self.viewCheckRstBtn = QtWidgets.QToolButton()
        self.viewCheckRstBtn.setObjectName('top_btn')
        self.viewCheckRstBtn.setText('结果浏览')
        self.viewCheckRstBtn.setIcon(QtGui.QIcon('./r1.jpg'))
        self.viewCheckRstBtn.setIconSize(QtCore.QSize(80,80))
        self.viewCheckRstBtn.setToolButtonStyle(QtCore.Qt.ToolButtonTextUnderIcon)

        self.exportCheckRstBtn = QtWidgets.QToolButton()
        self.exportCheckRstBtn.setObjectName('top_btn')
        self.exportCheckRstBtn.setText('结果导出')
        self.exportCheckRstBtn.setIcon(QtGui.QIcon('./r1.jpg'))
        self.exportCheckRstBtn.setIconSize(QtCore.QSize(80,80))
        self.exportCheckRstBtn.setToolButtonStyle(QtCore.Qt.ToolButtonTextUnderIcon)


        self.spacerLb = QtWidgets.QLabel() #间隔Label
        self.settingWidget = QtWidgets.QWidget()
        self.settingLayout = QtWidgets.QVBoxLayout()
        self.settingWidget.setLayout(self.settingLayout)
        self.settingLayout.setSpacing(0)
        self.settingLayout.setContentsMargins(0,0,0,0)

        self.minimizeBtn = QtWidgets.QPushButton()
        self.maximizeBtn = QtWidgets.QPushButton()
        self.closeBtn = QtWidgets.QPushButton()
        self.setHboxLayoutTop = QtWidgets.QHBoxLayout()
        self.setHboxLayoutTop.setContentsMargins(0,0,0,0)
        self.setHboxLayoutTop.addStretch(1)
        self.setHboxLayoutTop.addWidget(self.minimizeBtn)
        self.setHboxLayoutTop.addWidget(self.maximizeBtn)
        self.setHboxLayoutTop.addWidget(self.closeBtn)

        self.setBtn = QtWidgets.QPushButton()
        self.setBtn.setText('设置')
        self.helpBtn = QtWidgets.QPushButton()
        self.helpBtn.setText('帮助')
        self.setHboxLayoutBottom = QtWidgets.QHBoxLayout()
        self.setHboxLayoutBottom.setContentsMargins(0,0,0,0)
        self.setHboxLayoutBottom.addStretch(1)
        self.setHboxLayoutBottom.addWidget(self.setBtn)
        self.setHboxLayoutBottom.addWidget(self.helpBtn)

        self.settingLayout.addLayout(self.setHboxLayoutTop)
        self.settingLayout.addStretch(1)
        self.settingLayout.addLayout(self.setHboxLayoutBottom)

        self.topLayout.addWidget(self.iconLb,QtCore.Qt.AlignLeft|QtCore.Qt.AlignVCenter)
        self.topLayout.addWidget(self.newCheckTaskBtn)
        self.topLayout.addWidget(self.startCheckBtn)
        self.topLayout.addWidget(self.viewCheckRstBtn)
        self.topLayout.addWidget(self.exportCheckRstBtn)
        # self.topLayout.addWidget(self.spacerLb,0,5,1,1)
        self.topLayout.addWidget(self.settingWidget)

    def uiStyle(self):
        self.topWindow.setFixedHeight(120)
        # self.topLayout.setSpacing(0)
        self.minimizeBtn.setFixedSize(40,20)
        self.maximizeBtn.setFixedSize(40,20)
        self.closeBtn.setFixedSize(40,20)
        self.setBtn.setFixedSize(60,25)
        self.helpBtn.setFixedSize(60,25)
        # self.iconLb.set
        self.topWindow.setStyleSheet('''
            QWidget#top_window{
                background:blue;
            }
            QLabel#icon_label{
                color:white;
                border:none;
                font-size:28px;
            }
            QToolButton#top_btn{
                border:none;
                background:blue;
                font-size:16px;
                color:white;
            }
            ''')
        self.setAttribute(QtCore.Qt.WA_TranslucentBackground)
        self.setWindowFlag(QtCore.Qt.FramelessWindowHint)
        self.mainLayout.setSpacing(0)

    def mousePressEvent(self, event):
        if event.button() == QtCore.Qt.LeftButton:
            self.m_drag = True
            self.m_DragPosition = event.globalPos()-self.pos()
            event.accept()
            self.setCursor(QtGui.QCursor(QtCore.Qt.OpenHandCursor))

    def mouseMoveEvent(self, QMouseEvent):
        if QtCore.Qt.LeftButton and self.m_drag:
            self.move(QMouseEvent.globalPos()-self.m_DragPosition)
            QMouseEvent.accept()

    def mouseReleaseEvent(self, QMouseEvent):
        self.m_drag = False
        self.setCursor(QtGui.QCursor(QtCore.Qt.ArrowCursor))

    def settingWidgetEvent(self):
        self.closeBtn.clicked.connect(self.close)
        self.minimizeBtn.clicked.connect(self.showMinimized)
        self.maximizeBtn.clicked.connect(self.showMax)


    def showMax(self):
        if self.isMaximized() == True:
            self.showNormal()
        else:
            self.showMaximized()

    def showMaximized(self):
        super(CheckSoft, self).showMaximized()
        self.mainLayout.setContentsMargins(0,0,0,0)

    def showNormal(self):
        super(CheckSoft, self).showNormal()
        self.mainLayout.setContentsMargins(1,1,1,1)



def run():
    app = QtWidgets.QApplication(sys.argv)
    gui = CheckSoft()
    gui.show()
    sys.exit(app.exec())


if __name__ == '__main__':
    run()
